<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>航班列表</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span>航班日期：</span>
                <input id="keyFlightStartDate" name="keyFlightStartDate"  class="mini-datepicker"   format="yyyy-MM-dd"/>--<input id="keyFlightEndDate" name="keyFlightEndDate" class="mini-datepicker"  format="yyyy-MM-dd"/>

                <span class="separator"></span>
                <span>目的站：</span>
                <input id="keyCityId" name="keyCityId" class="mini-treeselect"
                       url="" multiSelect="false" valueField="id" textField="name"
                       parentField="pid" allowInput="true" valueFromSelect="true"  virtualScroll="true"
                       emptyText="请选择目的站..."
                       style="width: 200px;" expandOnLoad="0" required="false" nullItemText="请选择目的站..."/>

                <span class="separator"></span>
                <span>始发机场：</span>
                <div id="keyAirportId" name="keyAirportId" class="mini-combobox" style="width:150px;"  popupWidth="600" textField="name" valueField="id"
                     required="false" requiredErrorText="始发机场不能为空"
                     allowInput="true"
                     emptyText="始发机场名称">
                    <div property="columns">
                        <div header="机场名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                    </div>
                </div>

                <span class="separator"></span>
                <span>航空公司：</span>
                <div id="keyAirlinesId" name="keyAirlinesId"
                     class="mini-combobox"
                     style="width:250px;"
                     popupWidth="800" textField="name" valueField="id"
                     allowInput="true" required="false" requiredErrorText="航空公司不能为空"
                     emptyText="航空公司名称">
                    <div property="columns">
                        <div header="名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                        <div header="两字码" field="twoCode"></div>
                    </div>
                </div>

                <span class="separator"></span>
                <span>班次号：</span>
                <input id="keyFlightNo" name="keyFlightNo" class="mini-textbox" style="width: 100px;"/>

                <div style="margin-top: 5px"></div>
                <% if(so.hasPermission("bFlight:query")){%>
                    <a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
                <% } %>

                <% if(so.hasPermission("bFlight:queryCndClear")){%>
                    <a class="mini-button" onclick="clearSearch()">清空查询条件</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("bFlight:add")){%>
                    <a class="mini-button" iconCls="icon-add" onclick="showAddFlight()">添加</a>
                <% } %>

                <% if(so.hasPermission("bFlight:edit")){%>
                    <a class="mini-button" iconCls="icon-edit" onclick="showEditFlight()">编辑</a>
                <% } %>

                <% if(so.hasPermission("bFlight:delete")){%>
                    <a class="mini-button" iconCls="icon-remove" onclick="deleteFlight()">删除</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("bFlight:batchAdd")){%>
                    <a class="mini-button" iconCls="icon-add" onclick="showBatchAddFlight()">批量生成航班</a>
                <% } %>

                <% if(so.hasPermission("bFlight:upload")){%>
                    <a class="mini-button" iconCls="icon-upload" onclick="showUploadPanel()">导入航班</a>
                <% } %>

                <a href="${base!}/hymj/files/航班导入模板.rar" target="_blank">点击下载《航班模板》模板</a>
                <span class="separator"></span>

                <% if(so.hasPermission("bFlight:download")){%>
                    <#download_grid id="downloadGrid" theGridId="datagrid1" theFileName="航班信息表"/>
                <% } %>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="datagrid1" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/flight_manage/query" idField="id"
                 showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
                 allowResize="true"
                 multiSelect = "true"
                 sortField="flightDate"
                 sortOrder="desc"
                 allowCellWrap="true">
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div field="flightNo" width="140" headerAlign="center" align="center" allowSort="true">班次号</div>
                    <div field="flightDate" dateFormat="yyyy-MM-dd"  width="140" headerAlign="center" align="center" allowSort="true" renderer="ondayRenderer">航班日期</div>
                    <div field="planTakeOffTime" width="80" headerAlign="center" align="center" allowSort="true">起飞时间</div>
                    <div field="airportId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airport"}' renderer="commonRender">始发机场</div>
                    <div field="cityId" width="80" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目的站</div>
                    <div field="airlinesId" width="180" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                    <div field="model" width="140" headerAlign="center" align="center" allowSort="true">机型</div>
                    <div field="comment" width="140" headerAlign="center" align="center" allowSort="true">备注</div>
                    <div field="createTime" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">创建时间</div>
                    <div field="createUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">创建用户</div>
                </div>
            </div>
        </div>


        <div id="addOrUpdatePanel" class="mini-window" title="航班管理" style="width:600px;height:300px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">

            <div property="footer" style="padding:10px;text-align: right">
                <a id="clearBtn" style="margin-right: 5px" class="mini-button" onclick="clearForm()">清空</a>
                <a style="margin-right: 5px"  class="mini-button" iconCls="icon-save" onclick="saveFlight()">保存</a>
                <a  class="mini-button" iconCls="icon-close" onclick="closePanel('addOrUpdatePanel')">关闭</a>
            </div>

            <div id="flightForm">
                <input id="opFlag" name="opFlag" emptyText="操作标志"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="操作标志不能为空" />
                <input id="id" name="id"  class="mini-hidden" required="false" enabled="false" enabled="false" requiredErrorText="ID不能为空" />

                <span>目的站:</span>
                <input id="cityId" name="cityId" class="mini-treeselect"
                       url="" multiSelect="false" valueField="id" textField="name"
                       parentField="pid" allowInput="true" valueFromSelect="true"
                       emptyText="请选择目的站..." requiredErrorText="目的站不能为空"
                       style="width: 200px;" expandOnLoad="0" required="true" nullItemText="请选择目的站..."/>

                <span>始发机场:</span>
                <div id="airportId" name="airportId" class="mini-combobox" style="width:150px;"  popupWidth="450" textField="name" valueField="id"
                     required="true" requiredErrorText="始发机场不能为空"
                     allowInput="true"
                     emptyText="始发机场名称">
                    <div property="columns">
                        <div header="机场名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                    </div>
                </div>
                <div style="margin-top: 5px"></div>
                <span>航空公司:</span>
                <div id="airlinesId" name="airlinesId"
                     class="mini-combobox"
                     style="width:250px;"
                     popupWidth="600" textField="name" valueField="id"
                     allowInput="true" required="true" requiredErrorText="航空公司不能为空"
                     emptyText="航空公司名称" onvaluechanged="airlinesIdValueChanged">
                    <div property="columns">
                        <div header="名称" field="name"></div>
                        <div header="简称" field="shortName"></div>
                        <div header="两字码" field="twoCode"></div>
                    </div>
                </div>

                <span>班次号:</span>
                <input id="flightNo" name="flightNo" class="mini-textbox" style="width: 100px;" required="true" requiredErrorText="班次号不能为空"/>

                <div style="margin-top: 5px"></div>
                <span>航班日期:</span><input id="flightDate" name="flightDate" class="mini-datepicker" style="width:200px;" nullValue="null"
                       format="yyyy-MM-dd" showTime="false" showOkButton="true" showClearButton="false"
                       required="true" requiredErrorText="航班日期不能为空" />
                <span>起飞时间:</span>
                <input name="planTakeOffTime" class="mini-textbox" required="true" emptyText="请输入起飞时间" requiredErrorText="起飞时间不能为空"/>
                <span style="color: red;margin-left: 5px">如:17:35</span>

                <div style="margin-top: 5px"></div>
                <span>机型:</span>
                <input name="model"  class="mini-textbox" required="false" requiredErrorText="机型不能为空" emptyText="请输入机型" />

                <div style="margin-top: 5px"></div>
                <span>备注:</span>
                <input name="comment" class="mini-textarea" required="false" requiredErrorText="备注不能为空" style="width:400px;height: 50px;"/>

            </div>
        </div>

        <!--批量手动生成航班窗口-->
        <div id="batchAddFlightPanel" class="mini-window" title="手动生成航班" style="width:500px;height:300px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
            <div property="footer" style="padding:10px;text-align: right">
                <a style="margin-right: 5px"  class="mini-button" iconCls="icon-save" onclick="batchAddFlight()">保存</a>
                <a  class="mini-button" iconCls="icon-close" onclick="closePanel('batchAddFlightPanel')">关闭</a>
            </div>
            <div id="addFlightDateForm">

                <span>原航班日期:</span><input id="originalDate" name="originalDate" class="mini-datepicker" style="width:200px;" nullValue="null"
                                         format="yyyy-MM-dd" showTime="false" showOkButton="true" showClearButton="false"
                                         required="true" requiredErrorText="原航班日期不能为空" />

                <div style="margin-top: 5px"></div>
                <span>目标航班日期:</span><input id="targetDate" name="targetDate" class="mini-datepicker" style="width:200px;" nullValue="null"
                                            format="yyyy-MM-dd" showTime="false" showOkButton="true" showClearButton="false"
                                            required="true" requiredErrorText="目标航班日期不能为空" />

                <div style="margin-top: 5px"></div>
                <span style="color: red;font-weight: bold">
                    注:原航班日期为过去的已经存在航班的日期，目标航班日期为需要生成航班数据的日期
                </span>
            </div>
        </div>

        <!-- 批量上传航班 -->
        <div id="uploadPanel" class="mini-window" title="航班上传" style="width:400px;height:200px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true" >
            <div property="footer" style="padding:10px;text-align: right">
                <a  id="uploadBtn" name="uploadBtn" class="mini-button" iconCls="icon-upload" onclick="uploadSave()">上传</a>
            </div>
            <table style="width: 100%">
                <tr>
                    <td><label>航班文件:</label></td>
                    <td>
                        <form id="upload-form-add">
                            <input type="file" id="upLoadFlightFileData" name="upLoadFileData"  accept=".xls,.xlsx" />
                        </form>
                    </td>
                </tr>
            </table>
        </div>



    </body>

    <script type="text/javascript">
        mini.parse();

        let keyFlightStartDate = mini.get("keyFlightStartDate");
        let keyFlightEndDate = mini.get("keyFlightEndDate");
        let keyCityId = mini.get("keyCityId");
        let keyAirportId = mini.get("keyAirportId");
        let keyAirlinesId = mini.get("keyAirlinesId");
        let keyFlightNo = mini.get("keyFlightNo");

        let grid = mini.get("datagrid1");

        let addOrUpdatePanel = mini.get("addOrUpdatePanel");
        let flightForm = new mini.Form("#flightForm");

        let clearBtn = mini.get("clearBtn");

        let opFlag = mini.get("opFlag");
        let cityId = mini.get("cityId");
        let airportId = mini.get("airportId");
        let airlinesId = mini.get("airlinesId");
        let flightNo = mini.get("flightNo");
        let flightDate = mini.get("flightDate");
        let planTakeOffTime = mini.get("planTakeOffTime");
        let model = mini.get("model");
        let comment = mini.get("comment");

        // batchAddFlightPanel  addFlightDateForm  originalDate  targetDate
        let batchAddFlightPanel = mini.get("batchAddFlightPanel");
        let addFlightDateForm = new mini.Form("#addFlightDateForm");

        let originalDate = mini.get("originalDate");
        let targetDate = mini.get("targetDate");



        $(function () {
            //  目的市
            let keyCityIdJsonData = commonRenderDropList("city");
            keyCityId.loadList(keyCityIdJsonData,"id","pid");
            cityId.loadList(public_deepClone(keyCityIdJsonData),"id","pid");

            // 始发机场
            let keyAirportIdJsonData = commonRenderDropList("airport");
            keyAirportId.load(keyAirportIdJsonData);
            airportId.load(public_deepClone(keyAirportIdJsonData));

            // 航空公司
            let keyAirlinesIdJsonData = commonRenderDropList("airlines");
            keyAirlinesId.load(keyAirlinesIdJsonData);
            airlinesId.load(public_deepClone(keyAirlinesIdJsonData));

            // 加载列表数据
            grid.load();
        });


        /**
         * 根据条件进行查询
         */
        function search(){
            let form = new mini.Form("#queryForm");
            let data = form.getData(true);
            grid.load(data);
        }

        /**
         * 清空查询条件
         */
        function clearSearch(){
            let form = new mini.Form("#queryForm");
            form.clear();
        }

        /**
         * 关闭面板
         */
        function closePanel(panelId) {
            // 先清空
            flightForm.clear();
            // 再关闭
            let winObj = mini.get(panelId);
            winObj.hide();
        }

        function clearForm(){
            flightForm.clear();
        }

        /**
         * 显示添加航班面板
         */
        function showAddFlight(){
            opFlag.setValue(OPER_ADD());
            clearBtn.setVisible(true);
            public_showModelAtPos("addOrUpdatePanel", "center", "middle");
        }

        /**
         * 显示编辑航班面板
         */
        function showEditFlight(){
            let selectItems = grid.getSelecteds();
            if(selectItems.length == 0 || selectItems.length >1){
                showAlert("没有选择或选择了多条编辑的行，请选择单行进行编辑!");
                return;
            }
            flightForm.setData(selectItems[0]);
            opFlag.setValue(OPER_UPDATE());
            clearBtn.setVisible(false);
            public_showModelAtPos("addOrUpdatePanel", "center", "middle");
        }

        /**
         * 保存航班
         */
        function saveFlight(){
            flightForm.validate();
            if (flightForm.isValid() == false) {
                return;
            }

            let formData = flightForm.getData(true);

            // 检验一下航班号前两位防止有人恶意更改
            let flightNoValue = flightNo.getValue();
            let flightNoTwoCode = flightNoValue.substring(0,2);
            if(flightNoTwoCode != selectTowCode){
                showAlert("班次号前缀两位码不能更改，正确的前两位编码是【" + selectTowCode + "】");
                return;
            }

            let actionName = opFlag.getValue() == OPER_ADD() ? "insert" : "update";
            let ret = public_getServerData("${base}/pages/flight_manage/" + actionName, formData);
            if(ret.ok){
                showAlert(ret.message,function (){
                    // 刷新grid
                    grid.reload();
                    if(actionName == OPER_UPDATE()){
                        closePanel('addOrUpdatePanel');
                    }
                    else{
                        // 清空
                        flightForm.clear();
                    }
                });
            }
            else{
                showAlert(ret.message,function (){
                    return;
                });
            }
        }


        let selectTowCode;
        /**
         * 根据选择的航空公司设置班次号前缀
         * @param e
         */
        function airlinesIdValueChanged(e){
            selectTowCode = e.selected.twoCode;
            flightNo.setValue(selectTowCode);
        }


        /**
         * 删除选择的航班
         */
        function deleteFlight(){
             showAlert("为了保证信息的完整性，暂不提供删除功能。");
        }


        /**
         * 显示批量生成面板
         */
        function showBatchAddFlight(){
            public_showModelAtPos("batchAddFlightPanel", "center", "middle");
        }

        function batchAddFlight(){
            // batchAddFlightPanel  addFlightDateForm  originalDate  targetDate
            addFlightDateForm.validate();
            if (addFlightDateForm.isValid() == false) {
                return;
            }

            let addFlightDateFormData = addFlightDateForm.getData(true);
            let originalDate = addFlightDateFormData.originalDate;
            let targetDate = addFlightDateFormData.targetDate;

            if(originalDate == targetDate){
                showAlert("原航班日期与目标航班日期相同，无法生成相应目标日期的航班!");
                return;
            }

            if(originalDate > targetDate){
                showAlert("原航班日期不能大于目标航班日期!");
                return;
            }

            mini.confirm("确认要根据原航班日期生成目标日期的航班?", "航班生成提示", function(confirmAction){
                if(confirmAction == "ok"){
                    let ret = public_getServerData("${base}/pages/flight_manage/batchTargetDateFlightByOriginalDate",addFlightDateFormData);
                     showAlert(ret.message,function (){
                         addFlightDateForm.clear();
                         grid.reload();
                         closePanel('batchAddFlightPanel');
                     });
                }
            });

        }

        /**
         * 航班上传
         */
        function showUploadPanel(){
            let form_add = new mini.Form("#upload-form-add");
            form_add.clear();
            // 弹出添加窗口
            public_showModelAtPos("uploadPanel", "center", "middle");
        }

        /**
         * 上传模板文件
         */
        function uploadSave() {
            let fileInput = $('#upLoadFlightFileData').get(0).files[0];
            if(fileInput){
                mini.confirm("您确认上传航班数据?", "提示", function(confirmAction){
                    if(confirmAction == "ok"){
                        let messageid = mini.loading("正在上传校验及上传处理中，请稍后...","航班文件Excel上传");
                        flightAjaxFileUpload(messageid);
                    }
                });
            }else{
                mini.alert("没有选择要上传的航班信息数据Excel文件!");
            }
        }


        // 调用异步上传
        function flightAjaxFileUpload(messageid) {
            let formData = new FormData($("#upload-form-add")[0]);
            $.ajax({
                //接口地址
                url: '${base}/pages/flight_manage/upload' ,
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {

                    mini.hideMessageBox(messageid);

                    //成功的回调
                    mini.alert(data.message);

                    // 清空上传对象
                    clearUploadFile("upload-form-add");

                    // 重新刷新数据
                    grid.reload();

                    public_onCancel('uploadPanel');

                },
                error: function (returndata) {
                    mini.alert("网络异常，请重试！");
                }
            });
        }


    </script>

</html>